<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>Cesium初认识 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="1.vue项目常规撸起 2.下载cesium依赖​    npm install cesium --save  3.在vue工程的根目录下新建vue.config.js文件，内容如下：123456789101112131415161718192021222324252627282930313233343536373839const CopyWebpackPlugin &#x3D; require(&#39;copy">
<meta property="og:type" content="article">
<meta property="og:title" content="Cesium初认识">
<meta property="og:url" content="http://wangdongbo5.gitee.io/blog/2018/08/20/cesium%E6%93%8D%E4%BD%9C/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="1.vue项目常规撸起 2.下载cesium依赖​    npm install cesium --save  3.在vue工程的根目录下新建vue.config.js文件，内容如下：123456789101112131415161718192021222324252627282930313233343536373839const CopyWebpackPlugin &#x3D; require(&#39;copy">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2018-08-20T01:20:00.000Z">
<meta property="article:modified_time" content="2020-04-15T10:13:25.724Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Gis">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/blog/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/blog/css/style.css">

<meta name="generator" content="Hexo 4.2.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/blog/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/blog/">Home</a>
        
          <a class="main-nav-link" href="/blog/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/blog/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://wangdongbo5.gitee.io/blog"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-cesium操作" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/blog/2018/08/20/cesium%E6%93%8D%E4%BD%9C/" class="article-date">
  <time datetime="2018-08-20T01:20:00.000Z" itemprop="datePublished">2018-08-20</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/blog/categories/Gis/">Gis</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Cesium初认识
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="1-vue项目常规撸起"><a href="#1-vue项目常规撸起" class="headerlink" title="1.vue项目常规撸起"></a>1.vue项目常规撸起</h3><hr>
<h3 id="2-下载cesium依赖"><a href="#2-下载cesium依赖" class="headerlink" title="2.下载cesium依赖"></a>2.下载cesium依赖</h3><p>​    <code>npm install cesium --save</code></p>
<hr>
<h3 id="3-在vue工程的根目录下新建vue-config-js文件，内容如下："><a href="#3-在vue工程的根目录下新建vue-config-js文件，内容如下：" class="headerlink" title="3.在vue工程的根目录下新建vue.config.js文件，内容如下："></a>3.在vue工程的根目录下新建vue.config.js文件，内容如下：</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> CopyWebpackPlugin = <span class="built_in">require</span>(<span class="string">'copy-webpack-plugin'</span>)</span><br><span class="line"><span class="keyword">const</span> webpack = <span class="built_in">require</span>(<span class="string">'webpack'</span>)</span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">'path'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> debug = process.env.NODE_ENV !== <span class="string">'production'</span></span><br><span class="line"><span class="keyword">let</span> cesiumSource = <span class="string">'./node_modules/cesium/Source'</span></span><br><span class="line"><span class="keyword">let</span> cesiumWorkers = <span class="string">'../Build/Cesium/Workers'</span></span><br><span class="line"><span class="keyword">let</span> cesiumModel = <span class="string">"./src/views"</span>;</span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    publicPath: <span class="string">''</span>,</span><br><span class="line">    devServer: &#123;</span><br><span class="line">        port: <span class="number">9999</span></span><br><span class="line">    &#125;,</span><br><span class="line">    configureWebpack: &#123;</span><br><span class="line">        output: &#123;</span><br><span class="line">            sourcePrefix: <span class="string">' '</span></span><br><span class="line">        &#125;,</span><br><span class="line">        amd: &#123;</span><br><span class="line">            toUrlUndefined: <span class="literal">true</span></span><br><span class="line">        &#125;,</span><br><span class="line">        resolve: &#123;</span><br><span class="line">            alias: &#123;</span><br><span class="line">                <span class="string">'vue$'</span>: <span class="string">'vue/dist/vue.esm.js'</span>,</span><br><span class="line">                <span class="string">'@'</span>: path.resolve(<span class="string">'src'</span>),</span><br><span class="line">                <span class="string">'cesium'</span>: path.resolve(__dirname, cesiumSource)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        plugins: [</span><br><span class="line">            <span class="keyword">new</span> CopyWebpackPlugin([ &#123; <span class="attr">from</span>: path.join(cesiumSource, cesiumWorkers), <span class="attr">to</span>: <span class="string">'Workers'</span>&#125;]),</span><br><span class="line">            <span class="keyword">new</span> CopyWebpackPlugin([ &#123; <span class="attr">from</span>: path.join(cesiumSource, <span class="string">'Assets'</span>), <span class="attr">to</span>: <span class="string">'Assets'</span>&#125;]),</span><br><span class="line">            <span class="keyword">new</span> CopyWebpackPlugin([ &#123; <span class="attr">from</span>: path.join(cesiumSource, <span class="string">'Widgets'</span>), <span class="attr">to</span>: <span class="string">'Widgets'</span>&#125;]),</span><br><span class="line">            <span class="keyword">new</span> CopyWebpackPlugin([ &#123; <span class="attr">from</span>: path.join(cesiumModel, <span class="string">'models'</span>), <span class="attr">to</span>: <span class="string">'Model'</span>&#125;]),</span><br><span class="line">            <span class="keyword">new</span> CopyWebpackPlugin([ &#123; <span class="attr">from</span>: path.join(cesiumSource, <span class="string">'ThirdParty/Workers'</span>), <span class="attr">to</span>: <span class="string">'ThirdParty/Workers'</span>&#125;]),</span><br><span class="line">            <span class="keyword">new</span> webpack.DefinePlugin(&#123;</span><br><span class="line">                CESIUM_BASE_URL: <span class="built_in">JSON</span>.stringify(<span class="string">'./'</span>)</span><br><span class="line">            &#125;)</span><br><span class="line">        ]</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<hr>
<h3 id="4-在项目或组件中引入"><a href="#4-在项目或组件中引入" class="headerlink" title="4.在项目或组件中引入"></a>4.在项目或组件中引入</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">import Cesium from 'cesium/Cesium'</span><br><span class="line">import widget from 'cesium/Widgets/widgets.css'</span><br></pre></td></tr></table></figure>

<hr>
<h3 id="5-新建一个地球"><a href="#5-新建一个地球" class="headerlink" title="5.新建一个地球"></a>5.新建一个地球</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> url = <span class="string">"http://localhost:3000/&#123;z&#125;/&#123;x&#125;/&#123;y&#125;.jpg"</span>; <span class="comment">// 指向的是离线地图的地址</span></span><br><span class="line"><span class="keyword">let</span> matrixIds = [</span><br><span class="line">    <span class="string">"EPSG:4326:0"</span>,</span><br><span class="line">    <span class="string">"EPSG:4326:1"</span>,</span><br><span class="line">    <span class="string">"EPSG:4326:2"</span>,</span><br><span class="line">    <span class="string">"EPSG:4326:3"</span>,</span><br><span class="line">    <span class="string">"EPSG:4326:4"</span>,</span><br><span class="line">    <span class="string">"EPSG:4326:5"</span>,</span><br><span class="line">    <span class="string">"EPSG:4326:6"</span>,</span><br><span class="line">    <span class="string">"EPSG:4326:7"</span>,</span><br><span class="line">    <span class="string">"EPSG:4326:8"</span>,</span><br><span class="line">    <span class="string">"EPSG:4326:9"</span>,</span><br><span class="line">    <span class="string">"EPSG:4326:10"</span>,</span><br><span class="line">    <span class="string">"EPSG:4326:11"</span>,</span><br><span class="line">    <span class="string">"EPSG:4326:12"</span>,</span><br><span class="line">    <span class="string">"EPSG:4326:13"</span>,</span><br><span class="line">    <span class="string">"EPSG:4326:14"</span>,</span><br><span class="line">    <span class="string">"EPSG:4326:15"</span>,</span><br><span class="line">    <span class="string">"EPSG:4326:16"</span>,</span><br><span class="line">    <span class="string">"EPSG:4326:17"</span>,</span><br><span class="line">    <span class="string">"EPSG:4326:18"</span>,</span><br><span class="line">    <span class="string">"EPSG:4326:19"</span>,</span><br><span class="line">    <span class="string">"EPSG:4326:20"</span>,</span><br><span class="line">    <span class="string">"EPSG:4326:21"</span></span><br><span class="line">];</span><br><span class="line"><span class="comment">// 创建基于公司服务器的WMTS图层</span></span><br><span class="line"><span class="keyword">let</span> wmtsImageryProvider = <span class="keyword">new</span> Cesium.WebMapTileServiceImageryProvider(&#123;</span><br><span class="line">    url: <span class="string">"http://222.73.81.124:8083/geoserver/gwc/service/wmts"</span>, <span class="comment">//服务地址，如：'http://localhost:8080/geoserver/gwc/service/wmts'</span></span><br><span class="line">    layer: <span class="string">"GOOGLEP:world6"</span>, <span class="comment">//图层名称，如：'tasmania'</span></span><br><span class="line">    style: <span class="string">""</span>,</span><br><span class="line">    format: <span class="string">"image/png"</span>,</span><br><span class="line">    tileMatrixSetID: <span class="string">"EPSG:4326"</span>,</span><br><span class="line">    tileMatrixLabels: matrixIds,</span><br><span class="line">    tilingScheme: <span class="keyword">new</span> Cesium.GeographicTilingScheme(&#123;</span><br><span class="line">        <span class="comment">//此处很重要，很重要。。。</span></span><br><span class="line">        numberOfLevelZeroTilesX: <span class="number">2</span>,</span><br><span class="line">        numberOfLevelZeroTilesY: <span class="number">1</span></span><br><span class="line">    &#125;)</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">var</span> viewer = <span class="keyword">new</span> Cesium.Viewer(<span class="string">"container"</span>, &#123;</span><br><span class="line">    <span class="comment">// 需要进行可视化的数据源的集合</span></span><br><span class="line">    animation: <span class="literal">false</span>, <span class="comment">//是否显示动画控件</span></span><br><span class="line">    shouldAnimate: <span class="literal">true</span>,</span><br><span class="line">    homeButton: <span class="literal">false</span>, <span class="comment">//是否显示Home按钮</span></span><br><span class="line">    fullscreenButton: <span class="literal">false</span>, <span class="comment">//是否显示全屏按钮</span></span><br><span class="line">    baseLayerPicker: <span class="literal">false</span>, <span class="comment">//是否显示图层选择控件</span></span><br><span class="line">    geocoder: <span class="literal">true</span>, <span class="comment">//是否显示地名查找控件</span></span><br><span class="line">    timeline: <span class="literal">false</span>, <span class="comment">//是否显示时间线控件</span></span><br><span class="line">    sceneModePicker: <span class="literal">true</span>, <span class="comment">//是否显示投影方式控件</span></span><br><span class="line">    navigationHelpButton: <span class="literal">false</span>, <span class="comment">//是否显示帮助信息控件</span></span><br><span class="line">    infoBox: <span class="literal">false</span>, <span class="comment">//是否显示点击要素之后显示的信息</span></span><br><span class="line">    requestRenderMode: <span class="literal">true</span>, <span class="comment">//启用请求渲染模式</span></span><br><span class="line">    scene3DOnly: <span class="literal">false</span>, <span class="comment">//每个几何实例将只能以3D渲染以节省GPU内存</span></span><br><span class="line">    sceneMode: <span class="number">3</span>, <span class="comment">//初始场景模式 1 2D模式 2 2D循环模式 3 3D模式  Cesium.SceneMode</span></span><br><span class="line">    imageryProvider: wmtsImageryProvider</span><br><span class="line">    <span class="comment">// imageryProvider: new Cesium.UrlTemplateImageryProvider(&#123;</span></span><br><span class="line">    <span class="comment">//   url: url</span></span><br><span class="line">    <span class="comment">//   // layers: "nurc:Arc_Sample" // Here just give layer name</span></span><br><span class="line">    <span class="comment">// &#125;)</span></span><br><span class="line">  &#125;);</span><br><span class="line">viewer._cesiumWidget._creditContainer.style.display = <span class="string">"none"</span>;  <span class="comment">// 隐藏cesium版权标注</span></span><br><span class="line"> <span class="comment">// 如果默认请求网络地图地址  找到cesium源码，将createDefaultTerrainProviderViewModels函数返回一个空数组即可</span></span><br></pre></td></tr></table></figure>
<p>添加离线图层等信息的图层可以这样添加</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> writeMarble = viewer.imageryLayers.addImageryProvider(</span><br><span class="line">       <span class="keyword">new</span> Cesium.UrlTemplateImageryProvider(&#123;</span><br><span class="line">         url: <span class="string">"http://localhost:3000/&#123;z&#125;/&#123;x&#125;/&#123;y&#125;.png"</span>, <span class="comment">// 添加图层的路径</span></span><br><span class="line">         rectangle: Cesium.Rectangle.fromDegrees(<span class="number">75.0</span>, <span class="number">28.0</span>, <span class="number">67.0</span>, <span class="number">29.75</span>)  <span class="comment">// 添加图层的限制区域（默认添加所有区域）</span></span><br><span class="line">       &#125;)</span><br><span class="line">     );</span><br><span class="line">writeMarble.alpha = <span class="number">0.5</span>; <span class="comment">// 添加图层的透明度</span></span><br></pre></td></tr></table></figure>



<hr>
<h3 id="6-创建3d模型车子"><a href="#6-创建3d模型车子" class="headerlink" title="6.创建3d模型车子"></a>6.创建3d模型车子</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> scene = viewer.scene;</span><br><span class="line"><span class="keyword">var</span> modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(</span><br><span class="line">    Cesium.Cartesian3.fromDegrees(</span><br><span class="line">        <span class="number">-75.62898254394531</span>,</span><br><span class="line">        <span class="number">40.02804946899414</span>,</span><br><span class="line">        <span class="number">0.0</span></span><br><span class="line">    )</span><br><span class="line">);</span><br><span class="line"><span class="keyword">let</span> cmodel = Cesium.Model.fromGltf(&#123;</span><br><span class="line">    url: <span class="string">"Model/GroundVehicle/GroundVehicle.glb"</span>, <span class="comment">// 创建3d模型的地址，文件为glb格式文件，可在cesium源码中(Apps\SampleData\models)目录下找到cesium自带的glb模型 ，在vue.config.js里设置的, models文件夹应与本文件同级</span></span><br><span class="line">    modelMatrix: modelMatrix, <span class="comment">// 模型中心点</span></span><br><span class="line">    scale: <span class="number">200000.0</span> <span class="comment">// 模型放大倍数</span></span><br><span class="line">&#125;);</span><br><span class="line"><span class="keyword">var</span> model = scene.primitives.add(cmodel);<span class="comment">// 将模型添加至地球</span></span><br></pre></td></tr></table></figure>

<hr>
<h3 id="7-模仿官网创建3d飞机等模型"><a href="#7-模仿官网创建3d飞机等模型" class="headerlink" title="7. 模仿官网创建3d飞机等模型"></a>7. 模仿官网创建3d飞机等模型</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br></pre></td><td class="code"><pre><span class="line">createModel(url, height, viewer) &#123;</span><br><span class="line">    viewer.entities.removeAll();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> position = Cesium.Cartesian3.fromDegrees(<span class="number">121.48</span>, <span class="number">31.22</span>, height); <span class="comment">// 将经纬度坐标（经度，纬度，高度）转换为三维空间的位置</span></span><br><span class="line">    <span class="keyword">var</span> heading = Cesium.Math.toRadians(<span class="number">135</span>); <span class="comment">// 模型角度左右扭</span></span><br><span class="line">    <span class="keyword">var</span> pitch = <span class="number">0</span>; <span class="comment">// 模型角度上下摇</span></span><br><span class="line">    <span class="keyword">var</span> roll = <span class="number">0</span>; <span class="comment">// 模型角度左右摇</span></span><br><span class="line">    <span class="keyword">var</span> hpr = <span class="keyword">new</span> Cesium.HeadingPitchRoll(heading, pitch, roll);</span><br><span class="line">    <span class="keyword">var</span> orientation = Cesium.Transforms.headingPitchRollQuaternion(</span><br><span class="line">        position,</span><br><span class="line">        hpr</span><br><span class="line">    );</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> entity = viewer.entities.add(&#123;</span><br><span class="line">        name: url,</span><br><span class="line">        position: position,</span><br><span class="line">        orientation: orientation,</span><br><span class="line">        model: &#123;</span><br><span class="line">            uri: url,</span><br><span class="line">            minimumPixelSize: <span class="number">128</span>, <span class="comment">// 模型最小尺寸（px），结合缩放倍数使用</span></span><br><span class="line">            maximumScale: <span class="number">20000</span> <span class="comment">// 模型最大可缩放倍数</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">    viewer.trackedEntity = entity; <span class="comment">// 将视野锁定至添加的3d模型</span></span><br><span class="line">&#125;,</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">var</span> options = [</span><br><span class="line">        &#123;</span><br><span class="line">            text: <span class="string">"Aircraft"</span>,</span><br><span class="line">            onselect: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">                _this.createModel(<span class="string">"Model/CesiumAir/Cesium_Air.glb"</span>, <span class="number">5000.0</span>, viewer);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            text: <span class="string">"Ground Vehicle"</span>,</span><br><span class="line">            onselect: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">                _this.createModel(</span><br><span class="line">                    <span class="string">"Model/GroundVehicle/GroundVehicle.glb"</span>,</span><br><span class="line">                    <span class="number">0</span>,</span><br><span class="line">                    viewer</span><br><span class="line">                );</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            text: <span class="string">"Hot Air Balloon"</span>,</span><br><span class="line">            onselect: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">                _this.createModel(</span><br><span class="line">                    <span class="string">"Model/CesiumBalloon/CesiumBalloon.glb"</span>,</span><br><span class="line">                    <span class="number">1000.0</span>,</span><br><span class="line">                    viewer</span><br><span class="line">                );</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            text: <span class="string">"Milk Truck"</span>,</span><br><span class="line">            onselect: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">                _this.createModel(</span><br><span class="line">                    <span class="string">"Model/CesiumMilkTruck/CesiumMilkTruck.glb"</span>,</span><br><span class="line">                    <span class="number">0</span>,</span><br><span class="line">                    viewer</span><br><span class="line">                );</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            text: <span class="string">"Skinned Character"</span>,</span><br><span class="line">            onselect: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">                _this.createModel(<span class="string">"Model/CesiumMan/Cesium_Man.glb"</span>, <span class="number">0</span>, viewer);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            text: <span class="string">"Draco Compressed Model"</span>,</span><br><span class="line">            onselect: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">                _this.createModel(</span><br><span class="line">                    <span class="string">"Model/DracoCompressed/CesiumMilkTruck.gltf"</span>,</span><br><span class="line">                    <span class="number">0</span>,</span><br><span class="line">                    viewer</span><br><span class="line">                );</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    ];</span><br><span class="line"></span><br><span class="line">      Sandcastle.addToolbarMenu(options);  <span class="comment">// 浏览器报错Sandcastle is not defind，添加下拉框可选择模型切换</span></span><br><span class="line">      _this.createModel(<span class="string">"Model/CesiumAir/Cesium_Air.glb"</span>, <span class="number">5000.0</span>, viewer);</span><br></pre></td></tr></table></figure>

<p>解决Sandcatle报错，将Sandcastle挂载到window对象下面，在项目入口中引用以下js代码</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br></pre></td><td class="code"><pre><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"><span class="meta">    'use strict'</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> defaultAction;</span><br><span class="line">    <span class="keyword">var</span> bucket = <span class="built_in">window</span>.location.href;</span><br><span class="line">    <span class="keyword">var</span> pos = bucket.lastIndexOf(<span class="string">'/'</span>);</span><br><span class="line">    <span class="keyword">if</span> (pos &gt; <span class="number">0</span> &amp;&amp; pos &lt; (bucket.length - <span class="number">1</span>)) &#123;</span><br><span class="line">        bucket = bucket.substring(pos + <span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">window</span>.Sandcastle = &#123;</span><br><span class="line">        bucket : bucket,</span><br><span class="line">        declare : <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        &#125;,</span><br><span class="line">        highlight : <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        &#125;,</span><br><span class="line">        registered : [],</span><br><span class="line">        finishedLoading : <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="built_in">window</span>.Sandcastle.reset();</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span>(defaultAction) &#123;</span><br><span class="line">                <span class="built_in">window</span>.Sandcastle.highlight(defaultAction);</span><br><span class="line">                defaultAction();</span><br><span class="line">                defaultAction = <span class="literal">undefined</span>;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="built_in">document</span>.body.className = <span class="built_in">document</span>.body.className.replace(<span class="regexp">/(?:\s|^)sandcastle-loading(?:\s|$)/</span>, <span class="string">' '</span>);</span><br><span class="line">        &#125;,</span><br><span class="line">        addToggleButton : <span class="function"><span class="keyword">function</span>(<span class="params">text, checked, onchange, toolbarID</span>) </span>&#123;</span><br><span class="line">            <span class="built_in">window</span>.Sandcastle.declare(onchange);</span><br><span class="line">            <span class="keyword">var</span> input = <span class="built_in">document</span>.createElement(<span class="string">'input'</span>);</span><br><span class="line">            input.checked = checked;</span><br><span class="line">            input.type = <span class="string">'checkbox'</span>;</span><br><span class="line">            input.style.pointerEvents = <span class="string">'none'</span>;</span><br><span class="line">            <span class="keyword">var</span> label = <span class="built_in">document</span>.createElement(<span class="string">'label'</span>);</span><br><span class="line">            label.appendChild(input);</span><br><span class="line">            label.appendChild(<span class="built_in">document</span>.createTextNode(text));</span><br><span class="line">            label.style.pointerEvents = <span class="string">'none'</span>;</span><br><span class="line">            <span class="keyword">var</span> button = <span class="built_in">document</span>.createElement(<span class="string">'button'</span>);</span><br><span class="line">            button.type = <span class="string">'button'</span>;</span><br><span class="line">            button.className = <span class="string">'cesium-button'</span>;</span><br><span class="line">            button.appendChild(label);</span><br><span class="line"></span><br><span class="line">            button.onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">                <span class="built_in">window</span>.Sandcastle.reset();</span><br><span class="line">                <span class="built_in">window</span>.Sandcastle.highlight(onchange);</span><br><span class="line">                input.checked = !input.checked;</span><br><span class="line">                onchange(input.checked);</span><br><span class="line">            &#125;;</span><br><span class="line"></span><br><span class="line">            <span class="built_in">document</span>.getElementById(toolbarID || <span class="string">'toolbar'</span>).appendChild(button);</span><br><span class="line">        &#125;,</span><br><span class="line">        addToolbarButton : <span class="function"><span class="keyword">function</span>(<span class="params">text, onclick, toolbarID</span>) </span>&#123;</span><br><span class="line">            <span class="built_in">window</span>.Sandcastle.declare(onclick);</span><br><span class="line">            <span class="keyword">var</span> button = <span class="built_in">document</span>.createElement(<span class="string">'button'</span>);</span><br><span class="line">            button.type = <span class="string">'button'</span>;</span><br><span class="line">            button.className = <span class="string">'cesium-button'</span>;</span><br><span class="line">            button.onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">                <span class="built_in">window</span>.Sandcastle.reset();</span><br><span class="line">                <span class="built_in">window</span>.Sandcastle.highlight(onclick);</span><br><span class="line">                onclick();</span><br><span class="line">            &#125;;</span><br><span class="line">            button.textContent = text;</span><br><span class="line">            <span class="built_in">document</span>.getElementById(toolbarID || <span class="string">'toolbar'</span>).appendChild(button);</span><br><span class="line">        &#125;,</span><br><span class="line">        addDefaultToolbarButton : <span class="function"><span class="keyword">function</span>(<span class="params">text, onclick, toolbarID</span>) </span>&#123;</span><br><span class="line">            <span class="built_in">window</span>.Sandcastle.addToolbarButton(text, onclick, toolbarID);</span><br><span class="line">            defaultAction = onclick;</span><br><span class="line">        &#125;,</span><br><span class="line">        addDefaultToolbarMenu : <span class="function"><span class="keyword">function</span>(<span class="params">options, toolbarID</span>) </span>&#123;</span><br><span class="line">            <span class="built_in">window</span>.Sandcastle.addToolbarMenu(options, toolbarID);</span><br><span class="line">            defaultAction = options[<span class="number">0</span>].onselect;</span><br><span class="line">        &#125;,</span><br><span class="line">        addToolbarMenu : <span class="function"><span class="keyword">function</span>(<span class="params">options, toolbarID</span>) </span>&#123;</span><br><span class="line">            <span class="keyword">var</span> menu = <span class="built_in">document</span>.createElement(<span class="string">'select'</span>);</span><br><span class="line">            menu.className = <span class="string">'cesium-button'</span>;</span><br><span class="line">            menu.onchange = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">                <span class="built_in">window</span>.Sandcastle.reset();</span><br><span class="line">                <span class="keyword">var</span> item = options[menu.selectedIndex];</span><br><span class="line">                <span class="keyword">if</span> (item &amp;&amp; <span class="keyword">typeof</span> item.onselect === <span class="string">'function'</span>) &#123;</span><br><span class="line">                    item.onselect();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;;</span><br><span class="line">            <span class="built_in">document</span>.getElementById(toolbarID || <span class="string">'toolbar'</span>).appendChild(menu);</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (!defaultAction &amp;&amp; <span class="keyword">typeof</span> options[<span class="number">0</span>].onselect === <span class="string">'function'</span>) &#123;</span><br><span class="line">                defaultAction = options[<span class="number">0</span>].onselect;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>, len = options.length; i &lt; len; ++i) &#123;</span><br><span class="line">                <span class="keyword">var</span> option = <span class="built_in">document</span>.createElement(<span class="string">'option'</span>);</span><br><span class="line">                option.textContent = options[i].text;</span><br><span class="line">                option.value = options[i].value;</span><br><span class="line">                menu.appendChild(option);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        reset : <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">window</span>.location.protocol === <span class="string">'file:'</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">window</span>.confirm(<span class="string">"You must host this app on a web server.\nSee contributor's guide for more info?"</span>)) &#123;</span><br><span class="line">            <span class="built_in">window</span>.location = <span class="string">'https://github.com/CesiumGS/cesium/wiki/Contributor%27s-Guide'</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;());</span><br></pre></td></tr></table></figure>


      
    </div>
    <footer class="article-footer">
      <a data-url="http://wangdongbo5.gitee.io/blog/2018/08/20/cesium%E6%93%8D%E4%BD%9C/" data-id="ck916ogvb00018guc96xv9ukc" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog/tags/Gis/" rel="tag">Gis</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/blog/2020/04/15/hello-world/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Hello World
        
      </div>
    </a>
  
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Gis/">Gis</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/Gis/" rel="tag">Gis</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/blog/tags/Gis/" style="font-size: 10px;">Gis</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2020/05/">May 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2020/04/">April 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2018/08/">August 2018</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/blog/2020/05/24/%E6%B5%8B%E8%AF%95blog/">测试blog</a>
          </li>
        
          <li>
            <a href="/blog/2020/04/15/hello-world/">Hello World</a>
          </li>
        
          <li>
            <a href="/blog/2018/08/20/cesium%E6%93%8D%E4%BD%9C/">Cesium初认识</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/blog/" class="mobile-nav-link">Home</a>
  
    <a href="/blog/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/blog/fancybox/jquery.fancybox.css">

  
<script src="/blog/fancybox/jquery.fancybox.pack.js"></script>




<script src="/blog/js/script.js"></script>




  </div>
</body>
</html>